package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.ashGate;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic.AshGateDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.AshGateExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICAshGateVideoBindService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysUserPlantConnService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITAreaService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CCompanysServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CPdtsAuthServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.AshGateBindCoalYardVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetAshGateVo;
import com.xiaoxu.intranetweb.util.IllegalCharacterCheck;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

@Component
public class AshGateBu {
    @Autowired
    private ICAshGateVideoBindService ashGateVideoBindService;
    @Autowired
    private ITAreaService areaService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ISysUserPlantConnService userPlantConnService;
    @Autowired
    private CCompanysServiceImpl companysService;
    @Autowired
    private AshGateDic ashGateDic;
    @Autowired
    private ICProductsInfoService productsInfoService;

    public SzResult getAshGate(String ashGateName, String areaId, Integer page, Integer pageSize,String plantId) {

        if (page != null || pageSize != null) {
            Page<CAshGateVideoBind> page1 = new Page<>(page, pageSize);
            QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(ashGateName != null && !ashGateName.equals(""), "ASHGATE_NAME", ashGateName);
            queryWrapper.eq(areaId != null && !areaId.equals(""), "AREA_ID", areaId);
            queryWrapper.eq("ASHGATE_DELETED", 0);
            queryWrapper.orderByAsc("ASHGATE_ID");
            queryWrapper.eq("DATA_FROM",plantId);
            List<CAshGateVideoBind> ashGateVideoBindList = ashGateVideoBindService.page(page1, queryWrapper).getRecords();
            Map<String, Object> map = new HashMap<>();
            List<GetAshGateVo> ashGateVoList = new ArrayList<>();
            for (CAshGateVideoBind ashGateVideoBind : ashGateVideoBindList) {
                GetAshGateVo getAshGateVo = new GetAshGateVo();
                getAshGateVo.setId(ashGateVideoBind.getId());
                getAshGateVo.setAreaId(ashGateVideoBind.getAreaId());
                getAshGateVo.setAshgateId(ashGateVideoBind.getAshgateId());
                getAshGateVo.setAshgateName(ashGateVideoBind.getAshgateName());
                getAshGateVo.setAshgateShrtname(ashGateVideoBind.getAshgateShrtname());
                getAshGateVo.setAshgateBindVcrId(ashGateVideoBind.getAshgateBindVcrId());
                getAshGateVo.setAshgateBindChnlNo(ashGateVideoBind.getAshgateBindChnlNo());
                getAshGateVo.setAshgateType(ashGateVideoBind.getAshgateType());
                getAshGateVo.setAshgateSpecgravity(ashGateVideoBind.getAshgateSpecgravity());
                getAshGateVo.setAshgateCubage(ashGateVideoBind.getAshgateCubage());
                getAshGateVo.setAshgateTotalh(ashGateVideoBind.getAshgateTotalh());
                getAshGateVo.setAshgateConeh(ashGateVideoBind.getAshgateConeh());
                getAshGateVo.setAshgateAlarmLl(ashGateVideoBind.getAshgateAlarmLl());
                getAshGateVo.setAshgateAlarmL(ashGateVideoBind.getAshgateAlarmL());
                getAshGateVo.setAshgateAlarmH(ashGateVideoBind.getAshgateAlarmH());
                getAshGateVo.setAshgateAlarmHh(ashGateVideoBind.getAshgateAlarmHh());
                getAshGateVo.setAshgateCurHght(ashGateVideoBind.getAshgateCurHght());
                getAshGateVo.setAshgateCurWght(ashGateVideoBind.getAshgateCurWght());
                getAshGateVo.setAshgateCurQlty(ashGateVideoBind.getAshgateCurQlty());
                getAshGateVo.setAshgateAcqTm(ashGateVideoBind.getAshgateAcqTm());
                getAshGateVo.setAshgatePdtId(ashGateVideoBind.getAshgatePdtId());
//                       getAshGateVo.setAshgatePdtName();
                if (ashGateVideoBind.getAshgatePdtId() != null){
                    CProductsInfo productsInfo = productsInfoService.getById(ashGateVideoBind.getAshgatePdtId());
                    if (productsInfo != null){
                        getAshGateVo.setAshgatePdtName(productsInfo.getPdtinfPdt());
                    }
                }
                getAshGateVo.setAshgateMan(ashGateVideoBind.getAshgateMan());
                getAshGateVo.setAshgateCurHghtMan(ashGateVideoBind.getAshgateCurHghtMan());
                getAshGateVo.setAshgateCurHghtManOperator(ashGateVideoBind.getAshgateCurHghtManOperator());
                getAshGateVo.setVehicleCapacity(ashGateVideoBind.getVehicleCapacity());
                getAshGateVo.setEmergency(ashGateVideoBind.getEmergency());
                getAshGateVo.setSvrTmstamp(ashGateVideoBind.getSvrTmstamp());
                getAshGateVo.setCliTmstamp(ashGateVideoBind.getCliTmstamp());
                getAshGateVo.setAshgateDeleted(ashGateVideoBind.getAshgateDeleted());
                getAshGateVo.setAshgateNew(ashGateVideoBind.getAshgateNew());
                getAshGateVo.setDownedID1(ashGateVideoBind.getDownedID1());
                getAshGateVo.setDownedID2(ashGateVideoBind.getDownedID2());
                getAshGateVo.setDownedID3(ashGateVideoBind.getDownedID3());
                getAshGateVo.setDownedID4(ashGateVideoBind.getDownedID4());
                getAshGateVo.setDownedID5(ashGateVideoBind.getDownedID5());
                getAshGateVo.setDownedID6(ashGateVideoBind.getDownedID6());
                getAshGateVo.setDownedID7(ashGateVideoBind.getDownedID7());
                getAshGateVo.setDownedID8(ashGateVideoBind.getDownedID8());
                getAshGateVo.setDownedID9(ashGateVideoBind.getDownedID9());
                getAshGateVo.setDownedID10(ashGateVideoBind.getDownedID10());
                getAshGateVo.setDownedID11(ashGateVideoBind.getDownedID11());
                getAshGateVo.setDownedID12(ashGateVideoBind.getDownedID12());
                getAshGateVo.setDownedID13(ashGateVideoBind.getDownedID13());
                getAshGateVo.setDownedID14(ashGateVideoBind.getDownedID14());
                getAshGateVo.setDownedID15(ashGateVideoBind.getDownedID15());
                getAshGateVo.setDownedID16(ashGateVideoBind.getDownedID16());
                getAshGateVo.setDownedID17(ashGateVideoBind.getDownedID17());
                getAshGateVo.setDownedID18(ashGateVideoBind.getDownedID18());
                getAshGateVo.setDownedID19(ashGateVideoBind.getDownedID19());
                getAshGateVo.setDownedID20(ashGateVideoBind.getDownedID20());
                getAshGateVo.setDataFrom(ashGateVideoBind.getDataFrom());
                getAshGateVo.setAshgateSn(ashGateVideoBind.getAshgateSn());
                getAshGateVo.setAshgateSubjectId(ashGateVideoBind.getAshgateSubjectId());
                if (StringUtils.isNotEmpty(ashGateVideoBind.getAshgateSubjectId())){
                    CCompanys company = companysService.getById(ashGateVideoBind.getAshgateSubjectId());
                    getAshGateVo.setAshgateSubjectName(company.getCpyCo()+'.'+company.getCpyChico());
                }
                getAshGateVo.setAreaId(ashGateVideoBind.getAreaId());
                if (ashGateVideoBind.getAreaId() != null) {
                    QueryWrapper<TArea> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("ID", ashGateVideoBind.getAreaId());
                    queryWrapper1.eq("DATA_FROM",plantId);
                    TArea tArea = areaService.getOne(queryWrapper1);
                    if (tArea == null) {
                        return SzResult.error("区域不存在,请检查");
                    }
                    getAshGateVo.setAreaName(tArea.getAreaName());
                } else {
                    getAshGateVo.setAreaName("未知");
                }
                ashGateVoList.add(getAshGateVo);
            }
            map.put("list", ashGateVoList);
            QueryWrapper<CAshGateVideoBind> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ASHGATE_DELETED", 0);
            List<CAshGateVideoBind> tAreaList = ashGateVideoBindService.list(queryWrapper1);
            Long total = (long) tAreaList.size();
            map.put("total", total);
            return SzResult.success(map);
        } else {
            QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(ashGateName != null && !ashGateName.isEmpty(), "ASHGATE_NAME", ashGateName);
            queryWrapper.eq(areaId != null && !areaId.isEmpty(), "AREA_ID", areaId);
            queryWrapper.eq("ASHGATE_DELETED", 0);
            queryWrapper.orderByDesc("ASHGATE_ID");
            queryWrapper.eq("DATA_FROM",plantId);
            List<CAshGateVideoBind> ashGateVideoBindList = ashGateVideoBindService.list(queryWrapper);
            Map<String, Object> map = new HashMap<>();
            List<GetAshGateVo> ashGateVoList = new ArrayList<>();
            for (CAshGateVideoBind ashGateVideoBind : ashGateVideoBindList) {
                GetAshGateVo getAshGateVo = new GetAshGateVo();
                getAshGateVo.setId(ashGateVideoBind.getId());
                getAshGateVo.setAreaId(ashGateVideoBind.getAreaId());
                getAshGateVo.setAshgateId(ashGateVideoBind.getAshgateId());
                getAshGateVo.setAshgateName(ashGateVideoBind.getAshgateName());
                getAshGateVo.setAshgateShrtname(ashGateVideoBind.getAshgateShrtname());
                getAshGateVo.setAshgateBindVcrId(ashGateVideoBind.getAshgateBindVcrId());
                getAshGateVo.setAshgateBindChnlNo(ashGateVideoBind.getAshgateBindChnlNo());
                getAshGateVo.setAshgateType(ashGateVideoBind.getAshgateType());
                getAshGateVo.setAshgateSpecgravity(ashGateVideoBind.getAshgateSpecgravity());
                getAshGateVo.setAshgateCubage(ashGateVideoBind.getAshgateCubage());
                getAshGateVo.setAshgateTotalh(ashGateVideoBind.getAshgateTotalh());
                getAshGateVo.setAshgateConeh(ashGateVideoBind.getAshgateConeh());
                getAshGateVo.setAshgateAlarmLl(ashGateVideoBind.getAshgateAlarmLl());
                getAshGateVo.setAshgateAlarmL(ashGateVideoBind.getAshgateAlarmL());
                getAshGateVo.setAshgateAlarmH(ashGateVideoBind.getAshgateAlarmH());
                getAshGateVo.setAshgateAlarmHh(ashGateVideoBind.getAshgateAlarmHh());
                getAshGateVo.setAshgateCurHght(ashGateVideoBind.getAshgateCurHght());
                getAshGateVo.setAshgateCurWght(ashGateVideoBind.getAshgateCurWght());
                getAshGateVo.setAshgateCurQlty(ashGateVideoBind.getAshgateCurQlty());
                getAshGateVo.setAshgateAcqTm(ashGateVideoBind.getAshgateAcqTm());
                getAshGateVo.setAshgatePdtId(ashGateVideoBind.getAshgatePdtId());
                if (ashGateVideoBind.getAshgatePdtId() != null) {
                    CProductsInfo productsInfo = productsInfoService.getById(ashGateVideoBind.getAshgatePdtId());
                    if (productsInfo != null) {
                        getAshGateVo.setAshgatePdtName(productsInfo.getPdtinfPdt());
                    }
                }
                getAshGateVo.setAshgateMan(ashGateVideoBind.getAshgateMan());
                getAshGateVo.setAshgateCurHghtMan(ashGateVideoBind.getAshgateCurHghtMan());
                getAshGateVo.setAshgateCurHghtManOperator(ashGateVideoBind.getAshgateCurHghtManOperator());
                getAshGateVo.setSvrTmstamp(ashGateVideoBind.getSvrTmstamp());
                getAshGateVo.setCliTmstamp(ashGateVideoBind.getCliTmstamp());
                getAshGateVo.setAshgateDeleted(ashGateVideoBind.getAshgateDeleted());
                getAshGateVo.setAshgateNew(ashGateVideoBind.getAshgateNew());
                getAshGateVo.setDownedID1(ashGateVideoBind.getDownedID1());
                getAshGateVo.setDownedID2(ashGateVideoBind.getDownedID2());
                getAshGateVo.setDownedID3(ashGateVideoBind.getDownedID3());
                getAshGateVo.setDownedID4(ashGateVideoBind.getDownedID4());
                getAshGateVo.setDownedID5(ashGateVideoBind.getDownedID5());
                getAshGateVo.setDownedID6(ashGateVideoBind.getDownedID6());
                getAshGateVo.setDownedID7(ashGateVideoBind.getDownedID7());
                getAshGateVo.setDownedID8(ashGateVideoBind.getDownedID8());
                getAshGateVo.setDownedID9(ashGateVideoBind.getDownedID9());
                getAshGateVo.setDownedID10(ashGateVideoBind.getDownedID10());
                getAshGateVo.setDownedID11(ashGateVideoBind.getDownedID11());
                getAshGateVo.setDownedID12(ashGateVideoBind.getDownedID12());
                getAshGateVo.setDownedID13(ashGateVideoBind.getDownedID13());
                getAshGateVo.setDownedID14(ashGateVideoBind.getDownedID14());
                getAshGateVo.setDownedID15(ashGateVideoBind.getDownedID15());
                getAshGateVo.setDownedID16(ashGateVideoBind.getDownedID16());
                getAshGateVo.setDownedID17(ashGateVideoBind.getDownedID17());
                getAshGateVo.setDownedID18(ashGateVideoBind.getDownedID18());
                getAshGateVo.setDownedID19(ashGateVideoBind.getDownedID19());
                getAshGateVo.setDownedID20(ashGateVideoBind.getDownedID20());
                getAshGateVo.setDataFrom(ashGateVideoBind.getDataFrom());
                getAshGateVo.setAshgateSn(ashGateVideoBind.getAshgateSn());
                getAshGateVo.setAshgateSubjectId(ashGateVideoBind.getAshgateSubjectId());
                if (StringUtils.isNotEmpty(ashGateVideoBind.getAshgateSubjectId())){
                    CCompanys company = companysService.getById(ashGateVideoBind.getAshgateSubjectId());
                    getAshGateVo.setAshgateSubjectName(company.getCpyCo()+'.'+company.getCpyChico());
                }
                getAshGateVo.setAreaId(ashGateVideoBind.getAreaId());
                if (ashGateVideoBind.getAreaId() != null) {
                    QueryWrapper<TArea> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("ID", ashGateVideoBind.getAreaId());
                    queryWrapper1.eq("DATA_FROM",plantId);
                    TArea tArea = areaService.getOne(queryWrapper1);
                    if (tArea == null) {
                        return SzResult.error("区域不存在,请检查");
                    }
                    getAshGateVo.setAreaName(tArea.getAreaName());
                } else {
                    getAshGateVo.setAreaName("未知");
                }
                ashGateVoList.add(getAshGateVo);
            }
            map.put("list", ashGateVoList);
            map.put("total", ashGateVoList.size());
            return SzResult.success(map);
        }
    }


    //新增区域管理
    public SzResult addAshGate(String ashGateName, String ashGateShrtame, String ashGateBindChnlNo, String ashGateType, String areaId, String ashGateSpecGravity,
                               String ashGateCubage, String ashGateTotalh, String ashGateConeh, String ashGateAlarmLl, String ashGateAlarml, String ashGateAlarmh,
                               String ashGateAlarmHh, String ashGateCurHght, String ashGateCurWght, String ashGateCurQlty, String ashGateAcqTm, String ashGatePdtID,String ashSubjectId,Integer ashSn, String token,String plantId) throws Exception {
        if (IllegalCharacterCheck.isNameLawful(ashGateName)){
            return SzResult.error("装载点名称里有非法字符");
        }
        if (IllegalCharacterCheck.isNameLawful(ashGateShrtame)){
            return SzResult.error("装载点简称里有非法字符");
        }
        CAshGateVideoBind cAshGateVideoBind = new CAshGateVideoBind();
        String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
        if (!ashGateName.equals("")) {
            QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ASHGATE_NAME", ashGateName);
            queryWrapper.eq("DATA_FROM", plantId);
            CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
            if (ashGateVideoBind != null) {
                return SzResult.error("该名称已存在,请重新输入");
            }
            cAshGateVideoBind.setId(uuid);
            cAshGateVideoBind.setAshgateName(ashGateName);
        } else {
            return SzResult.error("名称不能为空");
        }
        if (ashGateShrtame != null && !ashGateShrtame.equals("")) {
            QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ASHGATE_NAME", ashGateName);
            queryWrapper.eq("DATA_FROM", plantId);
            CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
            if (ashGateVideoBind != null) {
                return SzResult.error("该简称已存在,请重新输入");
            }
            cAshGateVideoBind.setAshgateShrtname(ashGateShrtame);
        } else {
            return SzResult.error("简称不能为空");
        }

        if (ashGateBindChnlNo != null && !ashGateBindChnlNo.equals("")) {
            cAshGateVideoBind.setAshgateBindChnlNo(BigDecimal.valueOf(Long.parseLong(ashGateBindChnlNo)));

        }

        if (ashSubjectId != null && !ashSubjectId.equals("")) {
            cAshGateVideoBind.setAshgateSubjectId(ashSubjectId);

        }
        if (ashSn != null) {
            cAshGateVideoBind.setAshgateSn(ashSn);

        }
//        else {
//            return SzResult.error("绑定通道号不能为空");
//        }
        if (ashGateType != null && !ashGateType.equals("")) {
            cAshGateVideoBind.setAshgateType(BigDecimal.valueOf(Long.parseLong(ashGateType)));
        }
        if (areaId != null && !areaId.equals("")) {
            cAshGateVideoBind.setAreaId(areaId);
        }
        if (ashGateSpecGravity != null && !ashGateSpecGravity.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateSpecgravity(BigDecimal.valueOf(Double.parseDouble(ashGateSpecGravity)));
        }
        if (ashGateCubage != null && !ashGateCubage.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateCubage(BigDecimal.valueOf(Double.parseDouble(ashGateCubage)));
        }
        if (ashGateTotalh != null && !ashGateTotalh.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateTotalh(BigDecimal.valueOf(Double.parseDouble(ashGateTotalh)));
        }
        if (ashGateConeh != null && !ashGateConeh.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateConeh(BigDecimal.valueOf(Double.parseDouble(ashGateConeh)));
        }
        if (ashGateAlarmLl != null && !ashGateAlarmLl.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateAlarmLl(BigDecimal.valueOf(Double.parseDouble(ashGateAlarmLl)));
        }
        if (ashGateAlarml != null && !ashGateAlarml.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateAlarmL(BigDecimal.valueOf(Double.parseDouble(ashGateAlarml)));
        }
        if (ashGateAlarmh != null && !ashGateAlarmh.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateAlarmH(BigDecimal.valueOf(Double.parseDouble(ashGateAlarmh)));
        }
        if (ashGateAlarmHh != null && !ashGateAlarmHh.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateAlarmHh(BigDecimal.valueOf(Double.parseDouble(ashGateAlarmHh)));
        }
        if (ashGateCurHght != null && !ashGateCurHght.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateCurHght(BigDecimal.valueOf(Double.parseDouble(ashGateCurHght)));
        }
        if (ashGateCurWght != null && !ashGateCurWght.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateCurWght(BigDecimal.valueOf(Double.parseDouble(ashGateCurWght)));
        }
        if (ashGateCurQlty != null && !ashGateCurQlty.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            cAshGateVideoBind.setAshgateCurQlty(ashGateCurQlty);
        }
        if (ashGateAcqTm != null && !ashGateAcqTm.equals("")) {
            if (areaId == null || areaId.equals("")) {
                return SzResult.error("请先选择区域");
            }
            ashGateAcqTm = ashGateAcqTm.replace(" ", "T");
            cAshGateVideoBind.setAshgateAcqTm(LocalDateTime.parse(ashGateAcqTm));
        }
        if (ashGatePdtID != null && !ashGatePdtID.equals("")) {
            cAshGateVideoBind.setAshgatePdtId(ashGatePdtID);
        }
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
       queryWrapper.orderByDesc("ASHGATE_ID");
        queryWrapper.last("limit 1");
        queryWrapper.eq("DATA_FROM", plantId);
       CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
       if (ashGateVideoBind != null) {
           BigDecimal ashgateId = ashGateVideoBind.getAshgateId();
          String ashgateId1 = String.valueOf((ashgateId.intValue() + 1));
          cAshGateVideoBind.setAshgateId(BigDecimal.valueOf(Long.parseLong(ashgateId1)));
           cAshGateVideoBind.setAshgateSn(Integer.valueOf(ashgateId1));
      } else {
           cAshGateVideoBind.setId(String.valueOf(1));
//            cAshGateVideoBind.setAshgateId(uuid);
           cAshGateVideoBind.setAshgateSn(1);
      }
       if (ashSn !=null) {
           cAshGateVideoBind.setAshgateSn(ashSn);
       }
//        QueryWrapper<SysUserPlantConn> queryWrapper2 = new QueryWrapper<>();
//        queryWrapper2.eq("USER_ID", StpUtil.getLoginIdByToken(token).toString());
//        SysUserPlantConn one2 = userPlantConnService.getOne(queryWrapper2);
       // String plantId = one2.getPlantId();
        cAshGateVideoBind.setDataFrom(plantId);
        cAshGateVideoBind.setAshgateDeleted(0);
        ashGateVideoBindService.save(cAshGateVideoBind);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("新增区域管理", user.getTrueName(), "新增了区域名称为" + ashGateName + "的区域管理", plantId);
        return SzResult.Success();
    }


    //删除区域管理
    public SzResult deleteAshGate(String ashGateId, String token,String plantId) throws Exception {
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASHGATE_ID", ashGateId);
        queryWrapper.eq("ASHGATE_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        QueryWrapper<CProductsInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.apply("FIND_IN_SET({0}, PDTINF_SPE_SITELIST)", ashGateVideoBind.getAshgateId());
        queryWrapper1.eq("PDTINF_DELETED", 0);
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.last("limit 1");
        CProductsInfo cProductsInfo = productsInfoService.getOne(queryWrapper1);
        if (cProductsInfo != null) {
            return SzResult.error("该区域已被品种管理表引用,请先删除品种管理表的该区域");
        }
        ashGateVideoBind.setAshgateDeleted(1);
        ashGateVideoBind.setDownedID1(0);
        ashGateVideoBindService.updateById(ashGateVideoBind);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("删除区域管理", user.getTrueName(), "删除了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理", plantId);
        return SzResult.Success();
    }

    //修改区域管理
    @Transactional
    public SzResult updateAshGate(String ashGateId, String ashGateName, String ashGateShrtame, String ashGateBindChnlNo, String ashGateType, String areaId,
                                  String ashGateSpecGravity, String ashGateCubage, String ashGateTotalh, String ashGateConeh, String ashGateAlarmLl,
                                  String ashGateAlarml, String ashGateAlarmh, String ashGateAlarmHh, String ashGateCurHght, String ashGateCurWght,
                                  String ashGateCurQlty, String ashGateAcqTm, String ashGatePdtID,String ashSubjectId,Integer ashSn,String token,String plantId) throws Exception {
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASHGATE_ID", ashGateId);
        queryWrapper.eq("ASHGATE_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        if (ashGateVideoBind != null) {
            if (!ashGateVideoBind.getAshgateName().equals(ashGateName)) {
                if (ashGateName != null && !ashGateName.equals("")) {
                    QueryWrapper<CAshGateVideoBind> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("ASHGATE_NAME", ashGateName);
                    queryWrapper1.eq("ASHGATE_DELETED", 0);
                    queryWrapper1.ne("ASHGATE_ID", ashGateId);
                    queryWrapper1.eq("DATA_FROM", plantId);
                    CAshGateVideoBind ashGateVideoBind1 = ashGateVideoBindService.getOne(queryWrapper1);
                    if (ashGateVideoBind1 != null) {
                        return SzResult.error("该区域名称已存在");
                    }
                }
                ashGateVideoBind.setAshgateName(ashGateName);
            }


            if (ashGateVideoBind.getAshgateShrtname() !=null ) {
                if (!ashGateVideoBind.getAshgateShrtname().equals(ashGateShrtame)){
                if (ashGateShrtame != null && !ashGateShrtame.equals("")) {
                    QueryWrapper<CAshGateVideoBind> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("ASHGATE_SHRTNAME", ashGateShrtame);
                    queryWrapper2.eq("ASHGATE_DELETED", 0);
                    queryWrapper2.ne("ASHGATE_ID", ashGateId);
                    queryWrapper2.eq("DATA_FROM", plantId);
                    CAshGateVideoBind ashGateVideoBind1 = ashGateVideoBindService.getOne(queryWrapper2);
                    if (ashGateVideoBind1 != null) {
                        return SzResult.error("该区域简称已存在");
                    }
                  }
                }
                ashGateVideoBind.setAshgateShrtname(ashGateShrtame);
            }

            if (ashSubjectId != null && !ashSubjectId.equals("")) {
                ashGateVideoBind.setAshgateSubjectId(ashSubjectId);

            }
            if (ashSn != null) {
            if(ashGateVideoBind.getAshgateSn()!=null) {
                if (!ashGateVideoBind.getAshgateSn().equals(ashSn)) {
                    QueryWrapper<CProductsInfo> productsInfoQueryWrapper = new QueryWrapper<>();
                    productsInfoQueryWrapper.like("PDTINF_SPE_SITELIST", ashGateName);
                    for (CProductsInfo productsInfo : productsInfoService.list(productsInfoQueryWrapper)) {
                        int index = Arrays.asList(productsInfo.getPdtinfSpeSitelist().split(",")).indexOf(ashGateName);
                        String[] snList = productsInfo.getPdtinfSpeSnlist().split(",");
                        snList[index] = String.valueOf(ashSn);
                        String snString = String.join(",", snList);
                        productsInfo.setPdtinfSpeSnlist(snString);
                        productsInfoService.updateById(productsInfo);
                    }
                }
            }
                ashGateVideoBind.setAshgateSn(ashSn);
            }
            if (ashGateBindChnlNo != null && !ashGateBindChnlNo.equals("")) {
                ashGateVideoBind.setAshgateBindChnlNo(BigDecimal.valueOf(Long.parseLong(ashGateBindChnlNo)));
            }
            if (ashGateType != null && !ashGateType.equals("")) {
                ashGateVideoBind.setAshgateType(BigDecimal.valueOf(Long.parseLong(ashGateType)));
            }
            if (areaId != null && !areaId.equals("")) {
                ashGateVideoBind.setAreaId(areaId);
            }
            if (ashGateSpecGravity != null && !ashGateSpecGravity.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateSpecgravity(BigDecimal.valueOf(Double.parseDouble(ashGateSpecGravity)));
            }
            if (ashGateCubage != null && !ashGateCubage.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateCubage(BigDecimal.valueOf(Double.parseDouble(ashGateCubage)));
            }
            if (ashGateTotalh != null && !ashGateTotalh.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateTotalh(BigDecimal.valueOf(Double.parseDouble(ashGateTotalh)));
            }
            if (ashGateConeh != null && !ashGateConeh.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateConeh(BigDecimal.valueOf(Double.parseDouble(ashGateConeh)));
            }
            if (ashGateAlarmLl != null && !ashGateAlarmLl.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateAlarmLl(BigDecimal.valueOf(Double.parseDouble(ashGateAlarmLl)));
            }
            if (ashGateAlarml != null && !ashGateAlarml.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateAlarmL(BigDecimal.valueOf(Double.parseDouble(ashGateAlarml)));
            }
            if (ashGateAlarmh != null && !ashGateAlarmh.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateAlarmH(BigDecimal.valueOf(Double.parseDouble(ashGateAlarmh)));
            }
            if (ashGateAlarmHh != null && !ashGateAlarmHh.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateAlarmHh(BigDecimal.valueOf(Double.parseDouble(ashGateAlarmHh)));
            }
            if (ashGateCurHght != null && !ashGateCurHght.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateCurHght(BigDecimal.valueOf(Double.parseDouble(ashGateCurHght)));
            }
            if (ashGateCurWght != null && !ashGateCurWght.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateCurWght(BigDecimal.valueOf(Double.parseDouble(ashGateCurWght)));
            }
            if (ashGateCurQlty != null && !ashGateCurQlty.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateVideoBind.setAshgateCurQlty(ashGateCurQlty);
            }
            if (ashGateAcqTm != null && !ashGateAcqTm.equals("")) {
                if (areaId == null || areaId.equals("")) {
                    return SzResult.error("请先选择区域");
                }
                ashGateAcqTm = ashGateAcqTm.replace(" ", "T");
                ashGateVideoBind.setAshgateAcqTm(LocalDateTime.parse(ashGateAcqTm));
            }
            if (ashGatePdtID != null && !ashGatePdtID.equals("")) {
                ashGateVideoBind.setAshgatePdtId(ashGatePdtID);
            }
            ashGateVideoBind.setDownedID1(0);
            ashGateVideoBindService.updateById(ashGateVideoBind);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("修改区域管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理", plantId);

        }
        return SzResult.Success();
    }

    //是否手动料位
    public SzResult isManual(String ashGateId, Integer isOrNot, String token,String plantId) throws Exception {
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASHGATE_ID", ashGateId);
        queryWrapper.eq("ASHGATE_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        if (ashGateVideoBind != null) {
            if (isOrNot == 1) {
                ashGateVideoBind.setAshgateMan("1");//手动料位
            } else if (isOrNot == 0) {
                ashGateVideoBind.setAshgateMan("0");//自动料位
            }
            ashGateVideoBind.setDownedID1(0);
            ashGateVideoBindService.updateById(ashGateVideoBind);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("修改区域是否手动料位管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的是否手动料位管理权限", plantId);
        }
        return SzResult.Success();
    }

    //设置手动料位高度和操作员
    public SzResult setAshGateCurHghtMan(String ashGateId, String ashGateManualHeight, String token,String plantId) throws Exception {
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASHGATE_ID", ashGateId);
        queryWrapper.eq("ASHGATE_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        if (ashGateVideoBind != null) {
            String ashgateMan = ashGateVideoBind.getAshgateMan();
            if (ashgateMan.equals("0")) {
                return SzResult.error("该料位不是手动料位");
            }
            ashGateVideoBind.setAshgateCurHghtMan(ashGateManualHeight);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            ashGateVideoBind.setAshgateCurHghtManOperator(user.getTrueName());
            ashGateVideoBind.setDownedID1(0);
            ashGateVideoBindService.updateById(ashGateVideoBind);
            sysLogBu.logRecord("修改区域手动料位高度管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的手动料位高度管理权限", plantId);
        }
        return SzResult.Success();
    }


    /**
     * 导表
     */
    @Async
    public Future<List<AshGateExcelEn>> exportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, Boolean isHeader, String mode,
                                                    Boolean original, Map<String, String> pager, String sheetName,String areaId, String ashGateName, String ashGateBindChnlNo, String plantId) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<AshGateExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为AshGateExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        AshGateExcelEn ashGateExcelEn = AshGateExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("ashgateName")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateName(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("ashgateShrtname")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateShrtName(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("ashgateBindChnlNo")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateBindChnlNo(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("areaName")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAreaType(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("ashgateType")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateType(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("ashgateSpecgravity")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateSpecGravity(entry.getValue().toString());
                                }
                            }
//                                trucksAxles
                            if (entry.getKey().equals("ashgateCubage")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCubage(entry.getValue().toString());
                                }
                            }
//                                trucksLength
                            if (entry.getKey().equals("ashgateTotalh")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateTotalh(entry.getValue().toString());
                                }
                            }
//                                trucksMaster
                            if (entry.getKey().equals("ashgateConeh")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateConeh(entry.getValue().toString());
                                }
                            }
//                                trucksIdentity
                            if (entry.getKey().equals("ashgateAlarmLl")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmLl(entry.getValue().toString());
                                }
                            }
//                                trucksTell
                            if (entry.getKey().equals("ashgateAlarmL")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmL(entry.getValue().toString());
                                }
                            }
//                                trucksRegMan
                            if (entry.getKey().equals("ashgateAlarmH")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmH(entry.getValue().toString());
                                }
                            }
//                                trucksTag1
                            if (entry.getKey().equals("ashgateAlarmHh")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmHh(entry.getValue().toString());
                                }
                            }
//                                trucksTag2
                            if (entry.getKey().equals("ashgateCurHght")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurHght(entry.getValue().toString());
                                }
                            }
//                                trucksTag3
                            if (entry.getKey().equals("ashgateCurWght")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurWght(entry.getValue().toString());
                                }
                            }
//                                trucksDispatcher
                            if (entry.getKey().equals("ashgateCurQlty")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurQlty(entry.getValue().toString());
                                }
                            }
//                                trucksDispatchTm
                            if (entry.getKey().equals("ashgateAcqTm")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAcqTm(entry.getValue().toString());
                                }
                            }
//                                trucksRefTare
                            if (entry.getKey().equals("ashgateMan")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateMan(entry.getValue().toString());
                                }
                            }
//                                trucksRefNet
                            if (entry.getKey().equals("ashgateCurHghtMan")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurHghtMan(entry.getValue().toString());
                                }
                            }
//                                trucksAgainMinTm
                            if (entry.getKey().equals("ashgateCurHghtManOperator")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurHghtManOperator(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(ashGateExcelEn);
                    }
                    break;
                case "all":
                    QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("ASHGATE_DELETED", 0);
                    queryWrapper.like(ashGateName != null && !ashGateName.equals(""), "ASHGATE_NAME", ashGateName);
                    queryWrapper.eq(areaId != null && !areaId.equals(""), "AREA_ID", areaId);
                    queryWrapper.eq(plantId != null && !plantId.equals(""), "DATA_FROM", plantId);
                    queryWrapper.orderByDesc("ASHGATE_ID");
                    List<CAshGateVideoBind> cashGateVideoBinds = ashGateVideoBindService.list(queryWrapper);
                    for (CAshGateVideoBind cashGateVideoBind : cashGateVideoBinds) {
                        BigDecimal ashgateType = cashGateVideoBind.getAshgateType();
                        if (ashgateType == null) {
                            ashgateType = new BigDecimal(0);
                        } else {
                            ashgateType = cashGateVideoBind.getAshgateType();
                        }
                        String areaId1 = cashGateVideoBind.getAreaId();
                        String areaName;
                        if (areaId1 == null) {
                            areaId1 = "";
                            areaName = null;
                        } else {
                            areaId1 = cashGateVideoBind.getAreaId();
                            areaName = (areaService.getById(areaId1)).getAreaName();
                        }
                        AshGateExcelEn ashGateExcelEn = AshGateExcelEn.builder()
                                .ashGateName(Optional.ofNullable(cashGateVideoBind.getAshgateName()).orElse(""))
                                .ashGateShrtName(Optional.ofNullable(cashGateVideoBind.getAshgateShrtname()).map(Object::toString).orElse(""))
                                .ashGateBindChnlNo(Optional.ofNullable(cashGateVideoBind.getAshgateBindChnlNo()).map(Object::toString).orElse(""))
                                .ashGateType(Optional.ofNullable(ashGateDic.getAshGateTypeDic((ashgateType).intValue())).map(Object::toString).orElse(""))
                                .areaType(Optional.ofNullable(areaName).map(Object::toString).orElse(""))
                                .ashGateSpecGravity(Optional.ofNullable(cashGateVideoBind.getAshgateSpecgravity()).map(Object::toString).orElse(""))
                                .ashGateCubage(Optional.ofNullable(cashGateVideoBind.getAshgateCubage()).map(Object::toString).orElse(""))
                                .ashGateTotalh(Optional.ofNullable(cashGateVideoBind.getAshgateTotalh()).map(Object::toString).orElse(""))
                                .ashGateConeh(Optional.ofNullable(cashGateVideoBind.getAshgateConeh()).map(Object::toString).orElse(""))
                                .ashGateAlarmLl(Optional.ofNullable(cashGateVideoBind.getAshgateAlarmLl()).map(Object::toString).orElse(""))
                                .ashGateAlarmL(Optional.ofNullable(cashGateVideoBind.getAshgateAlarmL()).map(Object::toString).orElse(""))
                                .ashGateAlarmH(Optional.ofNullable(cashGateVideoBind.getAshgateAlarmH()).map(Object::toString).orElse(""))
                                .ashGateAlarmHh(Optional.ofNullable(cashGateVideoBind.getAshgateAlarmHh()).map(Object::toString).orElse(""))
                                .ashGateCurHght(Optional.ofNullable(cashGateVideoBind.getAshgateCurHght()).map(Object::toString).orElse(""))
                                .ashGateCurWght(Optional.ofNullable(cashGateVideoBind.getAshgateCurWght()).map(Object::toString).orElse(""))
                                .ashGateCurQlty(Optional.ofNullable(cashGateVideoBind.getAshgateCurQlty()).map(Object::toString).orElse(""))
                                .ashGateAcqTm(Optional.ofNullable(cashGateVideoBind.getAshgateAcqTm()).map(Object::toString).orElse(""))
                                .ashGateMan(Optional.ofNullable(ashGateDic.getAshGateManDic(cashGateVideoBind.getAshgateMan())).map(Object::toString).orElse(""))
                                .ashGateCurHghtMan(Optional.ofNullable(cashGateVideoBind.getAshgateCurHghtMan()).map(Object::toString).orElse(""))
                                .ashGateCurHghtManOperator(Optional.ofNullable(cashGateVideoBind.getAshgateCurHghtManOperator()).map(Object::toString).orElse(""))
                                .build();
                        list.add(ashGateExcelEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为AshGateExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        AshGateExcelEn ashGateExcelEn = AshGateExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("ashgateName")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateName(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("ashgateShrtname")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateShrtName(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("ashgateBindChnlNo")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateBindChnlNo(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("areaName")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAreaType(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("ashgateType")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateType(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("ashgateSpecgravity")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateSpecGravity(entry.getValue().toString());
                                }
                            }
//                                trucksAxles
                            if (entry.getKey().equals("ashgateCubage")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCubage(entry.getValue().toString());
                                }
                            }
//                                trucksLength
                            if (entry.getKey().equals("ashgateTotalh")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateTotalh(entry.getValue().toString());
                                }
                            }
//                                trucksMaster
                            if (entry.getKey().equals("ashgateConeh")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateConeh(entry.getValue().toString());
                                }
                            }
//                                trucksIdentity
                            if (entry.getKey().equals("ashgateAlarmLl")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmLl(entry.getValue().toString());
                                }
                            }
//                                trucksTell
                            if (entry.getKey().equals("ashgateAlarmL")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmL(entry.getValue().toString());
                                }
                            }
//                                trucksRegMan
                            if (entry.getKey().equals("ashgateAlarmH")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmH(entry.getValue().toString());
                                }
                            }
//                                trucksTag1
                            if (entry.getKey().equals("ashgateAlarmHh")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAlarmHh(entry.getValue().toString());
                                }
                            }
//                                trucksTag2
                            if (entry.getKey().equals("ashgateCurHght")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurHght(entry.getValue().toString());
                                }
                            }
//                                trucksTag3
                            if (entry.getKey().equals("ashgateCurWght")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurWght(entry.getValue().toString());
                                }
                            }
//                                trucksDispatcher
                            if (entry.getKey().equals("ashgateCurQlty")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurQlty(entry.getValue().toString());
                                }
                            }
//                                trucksDispatchTm
                            if (entry.getKey().equals("ashgateAcqTm")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateAcqTm(entry.getValue().toString());
                                }
                            }
//                                trucksRefTare
                            if (entry.getKey().equals("ashgateMan")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateMan(entry.getValue().toString());
                                }
                            }
//                                trucksRefNet
                            if (entry.getKey().equals("ashgateCurHghtMan")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurHghtMan(entry.getValue().toString());
                                }
                            }
//                                trucksAgainMinTm
                            if (entry.getKey().equals("ashgateCurHghtManOperator")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    ashGateExcelEn.setAshGateCurHghtManOperator(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(ashGateExcelEn);
                    }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }


    /**
     * 获取区域类型
     *
     * @param areaName
     * @param areaDesc
     * @param bz
     * @param token
     * @return
     */
    public SzResult getAreaType(String page, String pageSize, String areaName, String areaDesc, String bz, String token,String plantId) throws Exception {
        Map map = new HashMap();
        if (page == null || page.equals("") && pageSize == null || pageSize.equals("")) {
            QueryWrapper<TArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(areaName != null && !areaName.equals(""), "AREA_NAME", areaName);
            queryWrapper.like(areaDesc != null && !areaDesc.equals(""), "AREA_DESC", areaDesc);
            queryWrapper.like(bz != null && !bz.equals(""), "BZ", bz);
            queryWrapper.eq("DEL_FLAG", 0);
            queryWrapper.eq("DATA_FROM", plantId);
            List<TArea> list = areaService.list(queryWrapper);
            map.put("total", list.size());
            map.put("list", list);
        } else {
            Page<TArea> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
            QueryWrapper<TArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(areaName != null && !areaName.equals(""), "AREA_NAME", areaName);
            queryWrapper.like(areaDesc != null && !areaDesc.equals(""), "AREA_DESC", areaDesc);
            queryWrapper.like(bz != null && !bz.equals(""), "BZ", bz);
            queryWrapper.eq("DEL_FLAG", 0);
            queryWrapper.eq("DATA_FROM", plantId);
            IPage<TArea> iPage = areaService.page(page1, queryWrapper);
            List<TArea> list = iPage.getRecords();
            List<TArea> list1 = areaService.list(queryWrapper);
            int total = list1.size();
            map.put("total", total);
            map.put("list", list);
        }
        return SzResult.success(map);
    }

    /**
     * 新增区域类型
     *
     * @param areaName
     * @param areaDesc
     * @param bz
     * @param token
     * @return
     */
    public SzResult addAreaType(String areaName, String areaDesc, String bz, String token,String plantId) throws Exception {
        TArea tArea = new TArea();
        if (areaName != null && !areaName.equals("")) {
            QueryWrapper<TArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("AREA_NAME", areaName);
            queryWrapper.eq("DATA_FROM", plantId);
            TArea tArea1 = areaService.getOne(queryWrapper);
            if (tArea1 != null) {
                return SzResult.error("区域类型名称已存在");
            }
            tArea.setAreaName(areaName);
        } else {
            return SzResult.error("区域类型名称不能为空");
        }
        if (areaDesc != null && !areaDesc.equals("")) {
            tArea.setAreaDesc(areaDesc);
        }
        if (bz != null && !bz.equals("")) {
            tArea.setBz(bz);
        }
        tArea.setDelFlag(0);
        tArea.setDataFrom(plantId);
        areaService.save(tArea);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        //sysLogBu.logRecord("修改区域管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理");
        sysLogBu.logRecord("新增区域类型", user.getTrueName(), "新增了区域类型名称为" + areaName + "的区域类型", plantId);
        return SzResult.Success();
    }

    /**
     * 修改区域类型
     *
     * @param areaName
     * @param areaDesc
     * @param bz
     * @param id
     * @param token
     * @return
     */
    public SzResult updateAreaType(String areaName, String areaDesc, String bz, String id, String token,String plantId) throws Exception {
        if (id != null && !id.equals("")) {
            TArea tArea = areaService.getById(id);
            if (tArea != null) {
                if (areaName != null && !areaName.equals("")) {
                    QueryWrapper<TArea> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("AREA_NAME", areaName);
                    queryWrapper.ne("ID", id);
                    queryWrapper.eq("DATA_FROM", plantId);
                    TArea tArea1 = areaService.getOne(queryWrapper);
                    if (tArea1 != null) {
                        return SzResult.error("区域类型名称已存在");
                    }
                    tArea.setAreaName(areaName);
                }
                if (areaDesc != null && !areaDesc.equals("")) {
                    tArea.setAreaDesc(areaDesc);
                }
                if (bz != null && !bz.equals("")) {
                    tArea.setBz(bz);
                }
                areaService.updateById(tArea);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                // sysLogBu.logRecord("修改区域管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理");
                sysLogBu.logRecord("修改区域类型", user.getTrueName(), "修改了区域类型名称为" + tArea.getAreaName() + "的区域类型", plantId);
                return SzResult.Success();
            } else {
                return SzResult.error("区域类型不存在");
            }

        } else {
            return SzResult.error("区域类型id不能为空");
        }
    }

    public SzResult deleteAreaType(String id, String token,String plantId) throws Exception {
        if (id != null && !id.equals("")) {
            TArea tArea = areaService.getById(id);
            if (tArea != null) {
                tArea.setDelFlag(1);
                areaService.updateById(tArea);
                //日志
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                //sysLogBu.logRecord("修改区域管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理");
                sysLogBu.logRecord("删除区域类型", user.getTrueName(), "删除了区域类型名称为" + tArea.getAreaName() + "的区域类型", plantId);
                return SzResult.Success();
            } else {
                return SzResult.error("区域类型不存在");
            }
        } else {
            return SzResult.error("请选择要删除的区域类型");
        }
    }

//    //修改灰库可容车辆 @TableField("VEHICIE_CAPACITY")
////    private String vehicleCapacity;
//    @PostMapping("/updateAshGateVehicleCapacity")
//    public SzResult updateAshGateVehicleCapacity(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
//        String token = request.getHeader("Authorization");
//        String plantId = request.getHeader("Plant-Id");
//        String ashGateId = jsonObject.getString("ashGateId");
//        Integer ashGateCubage = jsonObject.getInteger("VehicleCapacity");
//        return ashGateBu.updateAshGateVehicleCapacity(ashGateId, ashGateCubage, token, plantId);
//    }
    public SzResult updateAshGateVehicleCapacity(String ashGateId, Integer VehicleCapacity, String token, String plantId) throws Exception {
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASHGATE_ID", ashGateId);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        if (ashGateVideoBind != null) {
            ashGateVideoBind.setVehicleCapacity(VehicleCapacity);
            ashGateVideoBindService.updateById(ashGateVideoBind);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("修改灰库可容车辆", user.getTrueName(), "修改了灰库名称为" + ashGateVideoBind.getAshgateName() + "的灰库可容车辆为"+"【"+VehicleCapacity+"】", plantId);
            return SzResult.Success();
        }
        return null;
    }

//    是否应急，emergency
    public SzResult updateAshGateEmergency(String ashGateId, Integer emergency, String token, String plantId) throws Exception {
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASHGATE_ID", ashGateId);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        if (ashGateVideoBind != null) {
            ashGateVideoBind.setEmergency(emergency);
            ashGateVideoBindService.updateById(ashGateVideoBind);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            if (emergency == 1) {
                sysLogBu.logRecord("修改库是否应急", user.getTrueName(), "打开了【" + ashGateVideoBind.getAshgateName() + "】的应急", plantId);
            }else{
                sysLogBu.logRecord("修改库是否应急", user.getTrueName(), "关闭了【" + ashGateVideoBind.getAshgateName() + "】的应急", plantId);
            }
            return SzResult.Success();
        }
        return SzResult.error("未找到库");
    }

    public SzResult updateAshGateAshgateSubject(String ashGateId, String ashgateSubjectId, String token, String plantId) throws Exception {
        QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASHGATE_ID", ashGateId);
        queryWrapper.eq("DATA_FROM", plantId);
        CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getOne(queryWrapper);
        if (ashGateVideoBind != null) {
            ashGateVideoBind.setAshgateSubjectId(ashgateSubjectId);
            ashGateVideoBindService.updateById(ashGateVideoBind);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("修改灰库所属发货单位", user.getTrueName(), "修改了【" + ashGateVideoBind.getAshgateName() + "】的所属单位", plantId);
            return SzResult.success("修改成功");
        }
        return SzResult.error("未找到库");
    }

    public SzResult getCoalGateList() {
        QueryWrapper<CAshGateVideoBind> ashGateVideoBindQueryWrapper = new QueryWrapper<>();
        ashGateVideoBindQueryWrapper.eq("AREA_ID","1397a7bae240383152dfa1161da93d2d");
        List<Map<String, Object>> list = ashGateVideoBindService.list(ashGateVideoBindQueryWrapper).stream().map(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id",item.getId());
            map.put("ashgateName", item.getAshgateName());
            return map;
        }).collect(Collectors.toList());
        return SzResult.success(list);
    }
}
